using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;

namespace HWGames.Bundles.Internal {
    public class HWSDKTime : SingletonMonoBehavior<HWSDKTime> {

        private const int MINUTES = 60;
        private const int TIME_LIMIT = 300;
        private const string MINUTES_EVENT = "minutes";
        private const string THOUSAND_MINUTES_EVENT = "minutes_thousand";

        private const string COUNT_MINUTE_KEY = "HWSDK_COUNT_MINUTE_KEY";
        private int m_countMinutes;
        public int CountMinutes {
            get => m_countMinutes;
            set {
                m_countMinutes = value;
                PlayerPrefs.SetInt(COUNT_MINUTE_KEY, value);
            }
        }

        private const string FIRST_HWSDK_TIME_KEY = "HWSDK_FIRST_TIME_KEY";
        private DateTime m_firstTime;
        public DateTime FirstTime {
            get => m_firstTime;
            set {
                m_firstTime = value;
                PlayerPrefs.SetString(FIRST_HWSDK_TIME_KEY, value.ToString());
            }
        }

        private const string COUNT_DAY_KEY = "HWSDK_COUNT_DAY_KEY";
        private int m_countDayKey;
        public int CountDayKey {
            get => m_countDayKey;
            set {
                m_countDayKey = value;
                PlayerPrefs.SetInt(COUNT_DAY_KEY, value);
            }
        }

        private float countTime = 0;

        private void Start() {
            CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");
            m_countDayKey = PlayerPrefs.GetInt(COUNT_DAY_KEY, 0);
            m_countMinutes = PlayerPrefs.GetInt(COUNT_MINUTE_KEY, -1);
            if (CountMinutes < 0) {
                FirstTime = DateTime.UtcNow.Date;
                CountMinutes = 0;
                LogMinutesEvent();
            }
            else {
                m_firstTime = DateTime.SpecifyKind(Convert.ToDateTime(PlayerPrefs.GetString(FIRST_HWSDK_TIME_KEY, DateTime.UtcNow.Date.ToString())), DateTimeKind.Utc);
            }
            if (CountDayKey <= 0) {
                int day = CheckDay();
                CountDayKey = day;
            }
        }

        private void Update() {
            UpdateTime();
        }

        private void UpdateTime() {
            if (CountMinutes >= TIME_LIMIT) {
                return;
            }
            countTime += Time.deltaTime;
            if (countTime >= MINUTES) {
                countTime = 0;
                CountMinutes++;
                LogMinutesEvent();
                if (CountDayKey <= 0) {
                    int day = CheckDay();
                    CountDayKey = day;
                }
            }
        }

        private void LogMinutesEvent() {
#if UNITY_EDITOR
            Debug.Log($"Day_Limit_{CountDayKey}_{CountMinutes}");
#endif
            SDKBundle.TrackCustomEvent(THOUSAND_MINUTES_EVENT, CountMinutes);
            if (CountDayKey > 0 || CountMinutes > 49)
                return;
#if UNITY_EDITOR
            Debug.Log($"Day_{CountDayKey}_{CountMinutes}");
#endif
            SDKBundle.TrackCustomEvent(MINUTES_EVENT, CountMinutes);
        }

        private int CheckDay() {
            TimeSpan daysSpan = DateTime.UtcNow.Date - FirstTime;
            return daysSpan.Days;
        }
    }
}